package com.loklok.code.confuse.util;

import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiArrayInitializerMemberValue;
import com.intellij.psi.PsiLiteralExpression;

import java.util.ArrayList;
import java.util.List;

public class AnnotationValueResolver {

    public static String getStringValue(PsiAnnotationMemberValue value) {
        if (value instanceof PsiLiteralExpression) {
            Object constantValue = ((PsiLiteralExpression) value).getValue();
            if (constantValue instanceof String) {
                return (String) constantValue;
            }
        }
        return null;
    }

    public static List<String> getArrayValues(PsiAnnotationMemberValue value) {
        List<String> result = new ArrayList<>();
        if (value instanceof PsiArrayInitializerMemberValue) {
            PsiAnnotationMemberValue[] arrayMembers = ((PsiArrayInitializerMemberValue) value).getInitializers();
            for (PsiAnnotationMemberValue member : arrayMembers) {
                String val = getStringValue(member);
                if (val != null) {
                    result.add(val);
                }
            }
        } else {
            String singleValue = getStringValue(value);
            if (singleValue != null) {
                result.add(singleValue);
            }
        }
        return result;
    }
}
